package com.aistar.controller;


import com.aistar.mapper.RewardMapper;
import com.aistar.pojo.Member;
import com.aistar.pojo.Reward;
import com.aistar.pojo.Travelnote;
import com.aistar.pojo.Vo.RewardRecordVo;
import com.aistar.service.*;
import org.hibernate.validator.constraints.pl.REGON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

@Controller
public class MyhomeController {
    @Autowired
    private MyHomeService myhomeService;
    @Autowired
    private BalanceService balanceService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private RewardMapper rewardMapper;
    @Autowired
    private RewardService rewardService;
    @Autowired
    private TravelnoteService travelnoteService;
    @Autowired
    private MyHomeService myHomeService;

    @RequestMapping("/myhome/list")
    public String list(Model model, Integer id) {
        List<RewardRecordVo> rewardRecordVos = new ArrayList<>();
        List<Reward> list1 = rewardService.getbyuserid(4);
        System.out.println("reward集合：" + list1);
        for(Reward reward:list1){
            Member member = myhomeService.selectByPrimaryKey(reward.getRewardUserId());
            rewardRecordVos.add(new RewardRecordVo(reward.getRewardOtherId(),member.getUserName(),reward.getRewardMoney(),reward.getRewardDate()));
        }
        Member member = memberService.getbyId(4);
        double rp = balanceService.selectById(4);
        int i = myhomeService.selectByUserIdNumber(4);
        System.out.println(i);
        int i2 = myhomeService.selectByAtenIdNumber(4);
        System.out.println(i2);
        model.addAttribute("member",member);
        model.addAttribute("money", rewardRecordVos);
        model.addAttribute("rewardprice",rp);
        model.addAttribute("fans",i);
        model.addAttribute("follow",i2);
        return "MyFoot/myhome";
    }

    @GetMapping("myhome/mytravel/{id}")
    public ModelAndView hometravel(@PathVariable("id") int id, HttpServletRequest request){

        //根据id获得用户的游记
        List<Travelnote> travelnoteList = travelnoteService.getByuserid(id);
        for (int i=travelnoteList.size()-1;i>=0;i--) {
            if(travelnoteList.get(i).getTravStatus()==0){
                travelnoteList.remove(i);
            }
        }
        Member member = memberService.getbyId(id);

        Object ss = request.getSession().getAttribute("userId");//获得session里的用户id
        int userId = Integer.parseInt(String.valueOf(ss));
        Member memberaa = memberService.getbyId(userId);

        int i=0;
        for(Travelnote aa:travelnoteList)
            i+=aa.getTravPraise();

        ModelAndView mav = new ModelAndView();
        mav.addObject("memberall",memberaa);
        mav.addObject("memtravel",travelnoteList);
        mav.addObject("userIn",member);
        mav.addObject("praise",i);
        mav.setViewName("MyFoot/mytravels");

        return mav;
    }

    @GetMapping("hishome/mytravel/{id}")
    public ModelAndView hidtravel(@PathVariable("id") int id,HttpServletRequest request){
        ModelAndView mav = new ModelAndView();
        //根据id获得用户的游记
        List<Travelnote> travelnoteList = travelnoteService.getByuserid(id);
        Object ss = request.getSession().getAttribute("userId");//获得session里的用户id
        int userId = Integer.parseInt(String.valueOf(ss));
        Member memberaa = memberService.getbyId(userId);

        mav.addObject("memberall",memberaa);
        Member member = memberService.getbyId(id);
        mav.addObject("memtravel",travelnoteList);
        mav.addObject("userIn",member);
        mav.setViewName("HisHome/Histravels");

        return mav;
    }

    //根据游记id删除用户
    @DeleteMapping("mytravel/delete")
    @ResponseBody
    public int delete(Integer travelid, HttpServletRequest request){
        System.out.println("根据游记id删除我的游记");
        System.out.println("游记id——"+travelid);
        int t = myHomeService.deleteByKey(travelid);

        System.out.println("开始删除");
        return t;


    }


}
